﻿using UnityEngine;
using System.Collections;

public class PlayerMoviment : MonoBehaviour
{
		public KeyCode jump;
		public KeyCode right;
		public KeyCode left;
		public KeyCode down;
		public KeyCode atk;
		public KeyCode paraCorrer;

		public float speedWalk;
		public float speedCorrendo;
		public float heightJump;
		public float parableJump = -1000;

		Vector3 originFlame;
		public float originFlameX;
		public float originFlameY;
		
		public Vector3 posOrigin;

		public bool jumping;		
		public bool completePouso = true;

		GameObject camera;
		public Transform flamePrefab;
		public GameObject pouso;

		Animator anim;
		// Use this for initialization
		void Start ()
		{
			anim = GetComponent<Animator>();
			camera = GameObject.FindGameObjectWithTag("MainCamera");
			posOrigin = transform.position;
		}
	
		// Update is called once per frame
		void Update ()
		{		
			originFlame = new Vector3(transform.position.x + originFlameX, transform.position.y + originFlameY, transform.position.z);
			if(pouso.GetComponent<Pouso>().pousando)
			{
				anim.SetBool("Pulando", false);
			}	
			else{
					if (Input.GetKey (right) && Input.GetKey (paraCorrer)) 
					{		
						if(!jumping){anim.SetTrigger("Correndo");}
						transform.Translate (speedCorrendo * Time.deltaTime, 0, 0);
						transform.localScale = new Vector3 (3, 3, 1);
					}
					else if (Input.GetKey (left) && Input.GetKey (paraCorrer)) 
					{
						if(!jumping){anim.SetTrigger("Correndo");}
						transform.Translate (-speedCorrendo * Time.deltaTime, 0, 0);
						transform.localScale = new Vector3 (-3, 3, 1);
					}
					else if (Input.GetKey (right)) 
					{
						if(!jumping){anim.SetTrigger("Andando");}
							transform.Translate (speedWalk * Time.deltaTime, 0, 0);
							transform.localScale = new Vector3 (3, 3, 1);
					}
					else if (Input.GetKey (left))
					{	
						if(!jumping){anim.SetTrigger("Andando");}
							transform.Translate (-speedWalk * Time.deltaTime, 0, 0);
							transform.localScale = new Vector3 (-3, 3, 1);
					}
					else if(!jumping){
						anim.SetTrigger("Parado");
					}
				
				}
				if(jumping)
				{
					transform.Translate (0, heightJump * Time.deltaTime, 0);
				}
				if(Input.GetKeyDown(atk))
				{
					Instantiate(flamePrefab, originFlame, Quaternion.identity);
				}
		}
		void OnCollisionStay2D(Collision2D other)
		{
			if(Input.GetKey(jump) && other.gameObject.tag == "chao")
			{
				jumping = true;
				anim.SetBool("Pulando", true);
			}
		}
		void OnCollisionEnter2D(Collision2D other)
		{
			if(other.gameObject.tag == "chao")
			{
				jumping = false;
			}
		}
		void OnTriggerEnter2D(Collider2D other)
		{
			if(other.gameObject.tag == "DeslPersiguiçãoDaCamera")
			{
				camera.GetComponent<CameraPos>().enabled = false;
			}
		}
}
